﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using FarseerPhysics;
using FarseerPhysics.Dynamics;
using FarseerPhysics.Collision;
using FarseerPhysics.Factories;
using FarseerPhysics.Common;
using FarseerPhysics.Common.Decomposition;
using FarseerPhysics.Common.ConvexHull;
using EpicBall.ScreenSystem;
using EpicBall.Camera;
using EpicBall.Levels.Classes;
using EpicBall.Levels.Classes.Enemies;

namespace EpicBall.Levels
{
    class Level1 : PhysicsGameScreen
    {
        int screenWidth;
        int screenHeight;

        Texture2D collisionTexture;
        Texture2D blankTexture;
        Texture2D waveTexture;

        Texture2D collectableTexture;
        Texture2D startTexture;
        Texture2D endTexture;

        Texture2D diamondTexture;
        Texture2D rampTexture;

        public Level1() : base(2000, 1200) { _leveltitle = "plains"; }

        public override void LoadContent()
        {
            base.LoadContent();
            screenWidth = (int)GameStack.Dimensions.X;
            screenHeight = (int)GameStack.Dimensions.Y;

            collisionTexture = GameStack.Content.Load<Texture2D>("test_level");
            ballTexture = GameStack.Content.Load<Texture2D>("character/ball1/face0");
            blankTexture = GameStack.Content.Load<Texture2D>("blank");
            
            playerTopArrowTexture = GameStack.Content.Load<Texture2D>("character/player_top_arrow");
            collectableTexture = GameStack.Content.Load<Texture2D>("ObstacleTextures/collectable");
            startTexture = GameStack.Content.Load<Texture2D>("ObstacleTextures/warpIn");
            endTexture = GameStack.Content.Load<Texture2D>("ObstacleTextures/warpOut");

            backgroundTexture = GameStack.Content.Load<Texture2D>("backgrounds/plains");

            waveTexture = GameStack.Content.Load<Texture2D>("ObstacleTextures/World/Sand/wave1");
            diamondTexture = GameStack.Content.Load<Texture2D>("ObstacleTextures/diamond");
            rampTexture = GameStack.Content.Load<Texture2D>("ObstacleTextures/ramp");

            screenSong = GameStack.Content.Load<Song>("Audio/plains");

            _airResistance = new Vector2(2.5f, 0);
            _speedThreshold = 12.5f;

            _levelGold = 30;
            _levelSilver = 20;
            _levelBronze = 10;

            gameWorld.Gravity = Vector2.UnitY * 10;


            LevelObject ground1 = new LevelObject(this, 1600, 30);
            ground1.Color = Color.Black;
            AddObjectToWorld(ground1, new Vector2(800, 400), 1);

            LevelObject diamond1 = PhysicsGameScreen.ImageToNewObjectPolygon(this, diamondTexture, gameWorld, 1f, 1f);
            AddObjectToWorld(diamond1, new Vector2(300, 400), 1);

            LevelObject diamond2 = PhysicsGameScreen.ImageToNewObjectPolygon(this, diamondTexture, gameWorld, 1f, 1f);
            AddObjectToWorld(diamond2, new Vector2(500, 200), 1);

            LevelObject diamond3 = PhysicsGameScreen.ImageToNewObjectPolygon(this, diamondTexture, gameWorld, 1f, 1f);
            AddObjectToWorld(diamond3, new Vector2(700, 300), 1);

            LevelObject diamond4 = PhysicsGameScreen.ImageToNewObjectPolygon(this, diamondTexture, gameWorld, 1f, 1f);
            AddObjectToWorld(diamond4, new Vector2(700, 100), 1);

            LevelObject diamond5 = PhysicsGameScreen.ImageToNewObjectPolygon(this, diamondTexture, gameWorld, 1f, 1f);
            AddObjectToWorld(diamond5, new Vector2(900, 400), 1);

            LevelObject ground2 = new LevelObject(this, 1000, 30);
            ground2.Color = Color.DarkGreen;
            AddObjectToWorld(ground2, new Vector2(1700, 800), 4);

            LevelObject ramp1 = PhysicsGameScreen.ImageToNewObjectPolygon(this, rampTexture, gameWorld, 1f, 1f);
            AddObjectToWorld(ramp1, new Vector2(1358, 750), 4);

            LevelObject ground3 = new LevelObject(this, 1500, 120);
            ground3.Color = Color.LightGreen;
            AddObjectToWorld(ground3, new Vector2(1500, 1150), 2);

            LevelObject ground4 = new LevelObject(this, 1500, 120);
            ground4.Color = Color.DarkRed;
            AddObjectToWorld(ground4, new Vector2(0, 1150), 3);

            LevelObject ground5 = new LevelObject(this, 400, 30);
            ground5.Color = Color.Black;
            AddObjectToWorld(ground5, new Vector2(0, 750), 3);
            

            Collectable collectable1 = new Collectable(this, collectableTexture);
            AddObjectToWorld(collectable1, new Vector2(500, 140), 0, 2, 3);

            Collectable collectable2 = new Collectable(this, collectableTexture);
            AddObjectToWorld(collectable2, new Vector2(700, 40), 0, 2, 3);

            Collectable collectable3 = new Collectable(this, collectableTexture);
            AddObjectToWorld(collectable3, new Vector2(700, 240), 0, 2, 3);

            Collectable collectable4 = new Collectable(this, collectableTexture);
            AddObjectToWorld(collectable4, new Vector2(600, 500), 0, 2, 3);

            Collectable collectable5 = new Collectable(this, collectableTexture);
            AddObjectToWorld(collectable5, new Vector2(1950, 760), 0, 2, 3);

            Collectable collectable6 = new Collectable(this, collectableTexture);
            AddObjectToWorld(collectable6, new Vector2(597, 650), 0, 2, 3);

            Collectable collectable7 = new Collectable(this, collectableTexture);
            AddObjectToWorld(collectable7, new Vector2(603, 700), 0, 2, 3);

            Collectable collectable8 = new Collectable(this, collectableTexture);
            AddObjectToWorld(collectable8, new Vector2(590, 800), 0, 2, 3);

            Collectable collectable9 = new Collectable(this, collectableTexture);
            AddObjectToWorld(collectable9, new Vector2(610, 750), 0, 2, 3);

            Collectable collectable10 = new Collectable(this, collectableTexture);
            AddObjectToWorld(collectable10, new Vector2(625, 775), 0, 2, 3);

            LevelObject start = new Warp(this, true, startTexture);
            AddObjectToWorld(start, new Vector2(100, 100), 0, 1, 0, true);

            LevelObject end = new Warp(this, false, endTexture);
            AddObjectToWorld(end, new Vector2(100, 700), 0, 1, 10, false);



            /*LevelObject waveBody = PhysicsGameScreen.ImageToPolygonBody(this, waveTexture, gameWorld, 1f, 1f);
            AddObjectToWorld(waveBody, new Vector2(600, 400), 1);
            
            SpikeTrap spiketrap = new SpikeTrap(this);
            AddObjectToWorld(spiketrap, new Vector2(150,300),0);

            StaticTurret turret = new StaticTurret(this,0.5f,false,true,1f);
            AddObjectToWorld(turret, new Vector2(800, 300), 1);
            StaticTurret turret2 = new StaticTurret(this, 0.5f, false, true, 1f);
            AddObjectToWorld(turret2, new Vector2(800, 200), 1);
            StaticTurret turret3 = new StaticTurret(this, 0.5f, false, true, 1f);
            AddObjectToWorld(turret3, new Vector2(800, 100), 1);

            StaticTurret peaceful_turret = new StaticTurret(this,3);
            AddObjectToWorld(peaceful_turret,new Vector2(1000, 300), 1);

            Door door = new Door(this,1);
            AddObjectToWorld(door, new Vector2(500, 300), 0);

            Key key = new Key(this, doors);
            AddObjectToWorld(key, new Vector2(300, 300), 0);

            MovingPlatform platform = new MovingPlatform(this, GameStack.Content.Load<Texture2D>("Enemies/projectilePlatform"));
            platform.AddPathNode(new MovingPlatform.AIPathNode(new Vector2(300,200)));
            platform.AddPathNode(new MovingPlatform.AIPathNode(new Vector2(350, 400)));
            platform.AddPathNode(new MovingPlatform.AIPathNode(new Vector2(500, 400)));
            platform.Ready();

            AddObjectToWorld(platform, new Vector2(300, 200), 1);*/

            //Setup positions
           
            _startPos = start.Body.Position;
            player.Position = _startPos;

        }


        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            if (isDead)
            {
                isDead = false;
                LoadContent();
            }
        }

        public override PhysicsGameScreen NextLevel()
        {
            return new Level2();
        }
    }
}
